package com.ruoyi.prt.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.WordPdfUtil;
import com.ruoyi.prt.domain.vo.PrtUserDocVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.prt.mapper.PrtUserDocMapper;
import com.ruoyi.prt.domain.PrtUserDoc;
import com.ruoyi.prt.service.IPrtUserDocService;

/**
 * 用户文档Service业务层处理
 * 
 * @author xiaobo Yan
 * @date 2021-03-31
 */
@Service
public class PrtUserDocServiceImpl implements IPrtUserDocService 
{
    @Autowired
    private PrtUserDocMapper prtUserDocMapper;

    /**
     * 查询用户文档
     * 
     * @param docId 用户文档ID
     * @return 用户文档
     */
    @Override
    public PrtUserDoc selectPrtUserDocById(String docId)
    {
        return prtUserDocMapper.selectPrtUserDocById(docId);
    }

    /**
     * 查询用户文档列表
     * 
     * @param prtUserDoc 用户文档
     * @return 用户文档
     */
    @Override
    public List<PrtUserDoc> selectPrtUserDocList(PrtUserDoc prtUserDoc)
    {
        return prtUserDocMapper.selectPrtUserDocList(prtUserDoc);
    }

    /**
     * 新增用户文档
     * 
     * @param prtUserDoc 用户文档
     * @return 结果
     */
    @Override
    public int insertPrtUserDoc(PrtUserDoc prtUserDoc)
    {
        return prtUserDocMapper.insertPrtUserDoc(prtUserDoc);
    }

    /**
     * 修改用户文档
     * 
     * @param prtUserDoc 用户文档
     * @return 结果
     */
    @Override
    public int updatePrtUserDoc(PrtUserDoc prtUserDoc)
    {
        return prtUserDocMapper.updatePrtUserDoc(prtUserDoc);
    }

    /**
     * 批量删除用户文档
     * 
     * @param docIds 需要删除的用户文档ID
     * @return 结果
     */
    @Override
    public int deletePrtUserDocByIds(String[] docIds)
    {
        return prtUserDocMapper.deletePrtUserDocByIds(docIds);
    }

    /**
     * 删除用户文档信息
     * 
     * @param docId 用户文档ID
     * @return 结果
     */
    @Override
    public int deletePrtUserDocById(String docId)
    {
        return prtUserDocMapper.deletePrtUserDocById(docId);
    }

    @Override
    public PageInfo<PrtUserDoc> selectPrtUserDocByCreated(PrtUserDocVO params) {
        PageHelper.startPage(params.getPageIndex(), params.getPageSize());
        List<PrtUserDoc> list = prtUserDocMapper.selectPrtUserDocByCreated(params.getCreated());
        return new PageInfo<>(list);
    }

    @Override
    public PrtUserDoc convertToPdf(PrtUserDoc doc) {
        String path = FileUploadUtils.getUploadDir(doc.getAddress());
        //转换pdf
        if(doc.getDocFormat().equals("pdf")) {
            doc.setPdfName(doc.getAddress());
            doc.setDocPage(WordPdfUtil.getPdfPage(FileUploadUtils.getUploadDir(doc.getAddress())));
        }
        else{
            String pdfName = FileUploadUtils.generatePdfName();
            String pdfPath = FileUploadUtils.getUploadDir(pdfName);
            WordPdfUtil.convertToPdfAsAspose(path, pdfPath);
            doc.setPdfName(pdfName);
            doc.setDocPage(WordPdfUtil.getPdfPage(pdfPath));
        }

        updatePrtUserDoc(doc);
        return doc;
    }
}
